import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:my_add_calendar/my_add_calendar.dart';
import 'package:my_add_calendar/Calendars.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Calendars calendars = Calendars(
    DateTime(2025, 5, 1, 12, 23),
    DateTime(2025, 5, 1, 12, 34),
    'hola',
    'note',
    [5],
    '1',
    1,
  );

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Center(
          child: Column(
            children: <Widget>[
              MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                child: Text('创建日程'),
                onPressed: () {
                  createEvent(calendars);
                },
              ),

              MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                child: Text('修改日程'),
                onPressed: () {
                  calendarsInit();
                  MyAddCalendar.updateEvent(calendars);
                },
              ),

              MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                child: Text('删除日程'),
                onPressed: () {
                  MyAddCalendar.deleteEvent(calendars.getEventId);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  void calendarsInit() {
    //更新参数
    calendars.setEventId = '113';
    calendars.setTitle = 'hola2';
    calendars.setAlert = [3, 15];
    calendars.setBeginTime = DateTime(2020, 5, 2, 12, 34);
    calendars.setEndTime = DateTime(2020, 5, 2, 12, 35);
    calendars.setNote = '这里不是备注内容/🐶';
  }

  Future<void> createEvent(Calendars calendars) async {
    print('calendars.eventId = ${calendars.getEventId}');
    //查询是否有读权限。
    await MyAddCalendar.checkReadPermission().then((res) async {
      if (res) {
        //查询是否有写权限
        await MyAddCalendar.checkWritePermission().then((resWrite) {
          if (resWrite) {
            MyAddCalendar.createEvent(calendars);
          }
        });
      }
    });
  }
}
